public static int iViewX, iViewY;
public static int s_iLevel = 1;
public Image imgInGameTile;
public Image imgItemTile;
public Image imgMainCharacter;
public Image imgWin;
public Image[] aimgSoftItem = new Image[20];
public Image[] monsterImage;
public static SuperMan s_MainCharacter;
public static LayerManager s_lmMap;
public Sprite sprMainCharacter, sprBomb;
public Player[] aPlayer = new Player[10]; 
public Player soundPutbom,soundExplosion;
//public byte[][] m_abMapLevel1,m_abMapLevel2,m_abMapLevel3;

void UpdateIngame(int mode)
{
	if (mode == UpdateType.UPDATE)
	{				
			//Switch Between levels		
		switch(s_iLevel)
		{
			case 1:
				UpdateLevel1(UpdateType.UPDATE);
				break;
			case 2: UpdateLevel2(UpdateType.UPDATE); break;
			case 3: 
				UpdateLevel3(UpdateType.UPDATE);
				break;						
		}							
	} //end of Update mode
	
	if (mode == UpdateType.PAINT)
	{
		// s_g.setColor(c_white);
		// s_g.fillRect(0,0,SCREEN_W, SCREEN_H);
		// s_g.setClip(0,0, VIEW_W, VIEW_H);		
		switch(s_iLevel){
			case 1: 
				UpdateLevel1(UpdateType.PAINT);
				break;
			case 2: UpdateLevel2(UpdateType.PAINT); break;
			case 3: 
				UpdateLevel3(UpdateType.PAINT);
				break;			
		}
		// s_g.setClip(0,SCREEN_H-40, SCREEN_W, SCREEN_H);
		// s_g.setColor(c_red);
		// s_g.drawRect(0, SCREEN_H-40, 239, 39);
		// s_g.drawLine(0, SCREEN_H-19, SCREEN_W, SCREEN_H-19);
		// s_g.setColor(c_blue);
		// s_g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE));
		// s_g.drawString("Life     "+s_MainCharacter.s_iLife,  SCREEN_W-50, SCREEN_H-15, Graphics.TOP | Graphics.LEFT);
		// s_g.drawString("Health   "+s_MainCharacter.m_iBlood,  SCREEN_W-50, SCREEN_H-30, Graphics.TOP | Graphics.LEFT);
		// if(s_MainCharacter.m_abBombkind[s_MainCharacter.iCurrentBomb+6]<0)
		// {	
			// s_g.drawString("x N\\A",  SCREEN_W-110, SCREEN_H-30, Graphics.TOP | Graphics.LEFT);
		// }
		// else
		// {
			// s_g.drawString("x  "+s_MainCharacter.m_abBombkind[s_MainCharacter.iCurrentBomb+6],  SCREEN_W-110, SCREEN_H-30, Graphics.TOP | Graphics.LEFT);
		// }
		// sprBomb.setFrame(s_MainCharacter.iCurrentBomb);
		// sprBomb.paint(s_g);
		
	}
	
	if (mode == UpdateType.CTOR)
	{
	    //PlaySound("/music.mid",2);
		try
		{
		//image for map tile
			imgInGameTile = Image.createImage("/tilesetSoild71.png");
			s_lmMap = new LayerManager();
		//create Main character
			imgMainCharacter = Image.createImage("/hero_strip8_strip32.png");		
			sprMainCharacter = new Sprite(imgMainCharacter, 24, 24);
			s_MainCharacter = new SuperMan(sprMainCharacter);
			s_MainCharacter.SetPosition(3, 2);
		//image for item
			imgItemTile = Image.createImage("/tiledItem_strip30.png");
			imgWin = Image.createImage("/win.png");
			sprBomb=new Sprite(Image.createImage("/bomb_strip5.png"),16,16);
			sprBomb.setPosition(SCREEN_W-125, SCREEN_H-35);
			
		//image for monster
			monsterImage = new Image[4];
			monsterImage[0] = Image.createImage("/monster0_strip40.png"); // This image is use to paint monster
			monsterImage[1] = Image.createImage("/monster1_strip40.png"); 
			monsterImage[2] = Image.createImage("/monster2_strip40.png"); 
			monsterImage[3] = Image.createImage("/monster3_strip40.png"); 			
			//monsterImage[4] = Image.createImage("/monster4_strip40.png"); 		
			
		} 
		catch (Exception e)
		{
			System.out.println("Error InGame-CTOR: " + e.getMessage());
		}
		
		switch(s_iLevel)
		{
			case 1: 
				UpdateLevel1(UpdateType.CTOR);
				break;
			case 2: UpdateLevel2(UpdateType.CTOR);
				break;
			case 3: 
				UpdateLevel3(UpdateType.CTOR);
				break;			
		}		
		
	if (mode== UpdateType.DTOR)
	{
	
		switch(s_iLevel)
		{
			case 1: 
				UpdateLevel1(UpdateType.DTOR);
				break;
			case 2:UpdateLevel2(UpdateType.DTOR);
				break;
			case 3: 
				UpdateLevel3(UpdateType.DTOR);
				break;
			
		}		
	}
	}	
}//end of IngameUpdate


public byte[][] ReadFileTiles(String arg, int length, int height) throws Exception
 {
	System.out.println("Reading file...");
	byte[][] ansMaps = null;	
	DataInputStream dis = new DataInputStream(getClass().getResourceAsStream(arg));
	
	ansMaps = new byte[height][length];
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < length; j++)
		{			 		 
			 ansMaps[i][j]=dis.readByte();					 
		}
	}
	dis.close();
	System.out.println("End reading");
	return ansMaps;	
}
public void CreateTiles (byte[][] data, TiledLayer tlBg, int X, int Y)
{	
int temp;
	 for(int i=0; i<X; i++)
		{		
		  for(int j=0; j<Y;j++)
		  {			    	 
			 temp=data[i][j];
			// System.out.print(temp + " ");			  
			 		  
			  if(temp<0)
				  temp = 255 & temp;
			  tlBg.setCell(j,i,temp);
		  }
		}
	tlBg.setPosition(0, 0);
	System.out.println("Create map oki");	
}

public void InitialItem ( byte[][] data, int[][]  ItemMap )
{
	int r;
	Random rand = new Random();
	
	    for(int i=0; i<data.length;i++)
		{	
			for(int j=0;j<data[0].length;j++)
			{
				if(data[i][j]>70|| data[i][j]<0)
				ItemMap[i][j] = SOLID;
				else ItemMap[i][j] = rand.nextInt(2)*FREE_CELL;	
				System.out.print("*"+ItemMap[i][j]);
			}
	   	    System.out.println();
		}  
} 
public void CreateMapItem ( byte[][] data, int[][] ItemMap )
{
	int w,h;	
	h=data.length;
	w=data[0].length;
	//System.out.println(w+":"+h);	
	    for(int i=0; i<h; i++)
		{	
			for(int j=0; j<w; j++)
			{
				if(data[i][j]>70 || data[i][j]<0)
				ItemMap[i][j] = SOLID;
				else ItemMap[i][j] = FREE_CELL;	
				//System.out.print("*"+ItemMap[i][j]);
				//System.out.print(" "+i+":"+j);
			}	   	    
		}
	System.out.println("Create ItemMap oki !");
}
void PlaySound(String fileName, int kind)
{
// kind = 2 : midi
// kind = 1 : x-wav
	try
	{				 
		if (hasSound)		
		{
		// case F_BG_MUSIC:
        if(kind==2)
				{
					//aPlayer[kind].stop();
					// aPlayer[kind] = Manager.createPlayer(getClass().getResourceAsStream(fileName),"audio/midi");
					// aPlayer[kind].setLoopCount(-1);
					// aPlayer[kind].realize();
				    // aPlayer[kind].prefetch();
				    aPlayer[kind].start();
				}
				
		}
					//aPlayer[kind].stop();
					// a = Manager.createPlayer(getClass().getResourceAsStream(fileName),"audio/x-wav");
					// a.realize();
				    // a.prefetch();
	    if(hasSfx)
		{
		if(kind==1)soundPutbom.start();
		if(kind==3)soundExplosion.start();
		}
	}
	catch(Exception e)
	{
		System.out.println("ERROR play sound !");
		e.printStackTrace();
	}
}
void StopSound(String fileName, int kind){
try{
    if(!hasSound)
	{
	 if(kind==2){
	 if(aPlayer[kind]==null)return;
	 aPlayer[kind].stop();
	 // aPlayer[kind].close();
	 // aPlayer[kind]=null;
	 }
    }
	if(!hasSfx) 
	 {if(kind==1)
	  {
	  if(soundPutbom==null)return;
	  soundPutbom.stop();
	  // soundPutbom.close();
	  // soundPutbom=null;
	  }
	  if(kind==3)
	  {
	  if(soundExplosion==null)return;
	  soundExplosion.stop();
	  // soundExplosion.close();
	  // soundExplosion=null;
	  }
     }
	 }
   catch(Exception e)
   {
   System.out.println("ERROR play sound !");
   e.printStackTrace();
   }
}
void loadSound(String fileName, int kind)
{
try
	{		
				if(kind==2)
				{
					//aPlayer[kind].stop();
					aPlayer[kind] = Manager.createPlayer(getClass().getResourceAsStream(fileName),"audio/midi");
					aPlayer[kind].setLoopCount(-1);
					aPlayer[kind].realize();
				    aPlayer[kind].prefetch();
				    //aPlayer[kind].start();
				}
				else if(kind==1)
				{
					//aPlayer[kind].stop();
					soundPutbom = Manager.createPlayer(getClass().getResourceAsStream(fileName),"audio/x-wav");
					soundPutbom.realize();
				    soundPutbom.prefetch();
					
				    //a.start();
				}		
                else
                {
				soundExplosion = Manager.createPlayer(getClass().getResourceAsStream(fileName),"audio/x-wav");
				soundExplosion.realize();
				soundExplosion.prefetch();
                }				
	}
	catch(Exception e)
	{
		System.out.println("ERROR play sound !");
		e.printStackTrace();
	}
}
public void Sort(int[] arr)
{	
	for(int i=0; i < arr.length/2; i++)
		for(int j=i; j<arr.length/2; j++)
		{
			if(arr[i] > arr[j])
			{//swap depth
				arr[i]+=arr[j];
				arr[j]=arr[i] - arr[j];
				arr[i]=arr[i] - arr[j];					
			//swap index
				arr[i+arr.length/2]+=arr[j+arr.length/2];
				arr[j+arr.length/2]=arr[i+arr.length/2] - arr[j+arr.length/2];
				arr[i+arr.length/2]=arr[i+arr.length/2] - arr[j+arr.length/2];	
			}
			
		}	
}
public static void PaintSoftKey(){
fonts[1].drawString("MENU",200,280,Graphics.LEFT,s_g);
}
